package com.sc.userspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.userspringboot.pojo.Games;
import com.sc.userspringboot.pojo.Notifications;
import com.sc.userspringboot.service.IGamesService;
import com.sc.userspringboot.service.INotificationsService;
import com.sc.userspringboot.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 * 公共游戏库基础信息表 前端控制器
 * </p>
 *
 * @author user
 * @since 2025-09-19 06:25:46
 */
@RestController
@RequestMapping("/games")
public class GamesController {

    @Autowired
    IGamesService gamesService;

    @Autowired
    INotificationsService notificationsService;

    @RequestMapping("detail")
    ResponseMsg gameDetail(@RequestParam("id") Integer gameId) {
        Games game = gamesService.getById(gameId);
        if (game == null) {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "游戏不存在");
        }
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, game);
    }

    // 推荐
    @RequestMapping("recommend")
    ResponseMsg recommend(@RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "6") Integer limit,
                          @RequestParam(required = false) Integer refreshKey) {
        // 获取所有游戏
        QueryWrapper<Games> wrapper = new QueryWrapper<>();
        List<Games> allGames = gamesService.list(wrapper);
        
        // 随机选择游戏
        Random random = new Random();
        int total = allGames.size();
        int start = (page - 1) * limit;
        
        // 创建分页结果
        IPage<Games> pageData = new Page<>(page, limit, total);
        
        // 如果请求的页面超出了总数据范围
        if (start >= total) {
            pageData.setRecords(List.of()); // 返回空列表
        } else {
            // 随机打乱游戏列表
            for (int i = 0; i < allGames.size(); i++) {
                int randomIndex = random.nextInt(allGames.size());
                Games temp = allGames.get(i);
                allGames.set(i, allGames.get(randomIndex));
                allGames.set(randomIndex, temp);
            }
            
            // 获取当前页的数据
            int end = Math.min(start + limit, total);
            List<Games> pageGames = allGames.subList(start, end);
            pageData.setRecords(pageGames);
        }
        
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, pageData);
    }

    // 榜单
    @RequestMapping("rank")
    ResponseMsg rank(@RequestParam String type,
                     @RequestParam(defaultValue = "1") Integer page,
                     @RequestParam(defaultValue = "10") Integer limit) {
        QueryWrapper<Games> wrapper = new QueryWrapper<>();
        switch (type) {
            //游戏上线时间
            case "releaseTime":
                wrapper.orderByDesc("release_time");
                break;
            //游戏更新时间
            case "updateTime":
                wrapper.orderByDesc("update_time");
                break;
            //游戏评分
            default:
                wrapper.orderByDesc("score");
        }
        IPage<Games> pageData = gamesService.page(new Page<>(page, limit), wrapper);
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, pageData);
    }

}